<template>
  <div class="register-container">
    <div class="system-title">
      <div class="logo">🧠📚</div>
      <h1>智能图书推荐系统</h1>
    </div>

    <div class="register-card">
      <h2>我要注册</h2>

      <el-form
        ref="registerForm"
        :model="formData"
        :rules="registerRules"
        label-position="top"
        @submit.native.prevent="handleRegister"
        size="medium"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            v-model="formData.username"
            placeholder="注册账号"
            :disabled="isLoading"
            show-word-limit
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item label="用户名" prop="name">
          <el-input
            v-model="formData.name"
            placeholder="用户名"
            :disabled="isLoading"
            show-word-limit
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="formData.password"
            type="password"
            placeholder="输入密码"
            :disabled="isLoading"
            show-word-limit
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="formData.confirmPassword"
            type="password"
            placeholder="确认密码"
            :disabled="isLoading"
            show-word-limit
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="register-button"
            :loading="isLoading"
            native-type="submit"
          >
            {{ isLoading ? '注册中...' : '立即注册' }}
          </el-button>
        </el-form-item>
        <div class="login-link">
          已有账户？<a href="#" @click.prevent="gotoLogin">返回登录</a>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { register } from '@/api/login'
import { registerRules } from '@/utils/validator'

export default {
  name: 'RegisterView',
  data() {
    return {
      formData: {
        username: '',
        name: '',
        password: '',
        confirmPassword: ''
      },
      registerRules,
      isLoading: false
    }
  },
  methods: {
    async handleRegister() {
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          try {
            this.isLoading = true
            // 调用注册接口
            const res = await register({
              username: this.formData.username,
              name: this.formData.name,
              password: this.formData.password,
              confirmPassword: this.formData.confirmPassword
            })

            if (res.code === '200') {
              this.$message({
                message: '注册成功！',
                type: 'success'
              })
              this.$router.push('/login')
            } else {
              this.$message({
                message: res.msg || '注册失败，请稍后重试！',
                type: 'error'
              })
            }
          } catch (error) {
            console.log(error)
          } finally {
            this.isLoading = false
          }
        }
      })
    },
    gotoLogin() {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
:deep(.el-form--label-top .el-form-item__label) {
  padding: 0 0 6px 0 !important;
  font-weight: 500;
  font-size: 14px;
  line-height: 1.4;
}

:deep(.el-input__inner) {
  box-sizing: border-box;
  height: 36px;
  font-size: 14px;
}

:deep(.el-form-item) {
  margin-bottom: 23px;
}

:deep(.el-button--primary) {
  width: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  height: 38px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 600;
  transition: all 0.3s ease;
  margin-bottom: 16px;
  padding: 0;
}

:deep(.el-button--primary:hover:not(:disabled)) {
  background: linear-gradient(135deg, #778cee 0%, #865bb2 100%);
}

:deep(.el-button--primary:disabled) {
  opacity: 0.8;
  cursor: not-allowed;
}

.register-container {
  width: 100vw;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: linear-gradient(135deg, #4e54c8 0%, #8f94fb 100%);
}

.system-title {
  text-align: center;
  margin-bottom: 30px;
  color: white;
}

.logo {
  font-size: 40px;
  margin-bottom: 8px;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}

.system-title h1 {
  color: white;
  font-size: 24px;
  font-weight: 600;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin: 0;
}

.register-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
  padding: 28px 24px;
  max-width: 360px;
  width: 100%;
}

.register-card h2 {
  color: #333;
  font-size: 20px;
  margin: 0 0 24px 0;
  font-weight: 600;
  text-align: center;
}

.login-link {
  text-align: center;
  color: #666;
  font-size: 13px;
  margin-top: 0;
}

.login-link a {
  color: #667eea;
  text-decoration: none;
  transition: color 0.3s ease;
}

.login-link a:hover {
  text-decoration: underline;
}
</style>
